﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.BIM;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	public partial class FrmSetInsulationLayer : System.Windows.Forms.Form
	{
		public FrmSetInsulationLayer(UIDocument uiDocument)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.uiDoc = uiDocument;
			this.dgvHeatRatio.AutoGenerateColumns = false;
			WallInfoOperate.SetWallTypeDefaultName(this.uiDoc.Document, 1.1);
			this.listHR = WallInfoOperate.GetAllWallHeatRatio();
			this.wts = WallInfoOperate.GetTermWallType(this.uiDoc.Document, 0, 1);
		}

		private void FrmSetInsulationLayer_Load(object sender, EventArgs e)
		{
			this.listNeedHR = this.GetDocWallType(this.listHR, this.wts);
			this.dgvHeatRatio.DataSource = this.listNeedHR;
			foreach (object obj in ((IEnumerable)this.dgvHeatRatio.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				dataGridViewRow.Cells["ColCheck"].Value = true;
                dataGridViewRow.Cells["ColStandardRatio"].Style.BackColor = System.Drawing.Color.Yellow;
			}
		}

		private IList<HeatRatio> GetDocWallType(IList<HeatRatio> listHR, List<WallType> wts)
		{
			IList<HeatRatio> list = new List<HeatRatio>();
			double wallThermal = this.GetWallThermal(listHR);
			foreach (WallType wallType in wts)
			{
				HeatRatio heatRatio = new HeatRatio();
				heatRatio.WallName = wallType.Name;
				foreach (HeatRatio heatRatio2 in listHR)
				{
					if (heatRatio2.WallName == wallType.Name)
					{
						heatRatio.KValue = heatRatio2.KValue;
						break;
					}
				}
				FrmCheckWall frmCheckWall = new FrmCheckWall(this.uiDoc, true);
				heatRatio.StandardValue = frmCheckWall.GetWallHeatRatio(this.uiDoc.Document, 3);
				heatRatio.LayerThick = FrmSetInsulationLayer.GetLayerThick(heatRatio.KValue, heatRatio.StandardValue, wallThermal);
				list.Add(heatRatio);
			}
			return list;
		}

		public double GetWallThermal(IList<HeatRatio> listHR)
		{
			double result = 0.0;
			foreach (HeatRatio heatRatio in listHR)
			{
				if (heatRatio.WallName == "保温导热系数")
				{
					result = heatRatio.KValue;
					break;
				}
			}
			return result;
		}

		private IList<HeatRatio> SetHeatRatioByGridView(IList<HeatRatio> listHR, DataGridView dgv)
		{
			foreach (object obj in ((IEnumerable)dgv.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				string b = dataGridViewRow.Cells["ColWallTypeName"].Value.ToString();
				DataGridViewCell dataGridViewCell = dataGridViewRow.Cells["ColCheck"] as DataGridViewCheckBoxCell;
				bool isChecked = false;
				string a = dataGridViewCell.FormattedValue.ToString();
				if (!(a == "False"))
				{
					if (a == "True")
					{
						isChecked = true;
					}
				}
				else
				{
					isChecked = false;
				}
				foreach (HeatRatio heatRatio in listHR)
				{
					if (heatRatio.WallName == b)
					{
						heatRatio.IsChecked = isChecked;
					}
				}
			}
			return listHR;
		}

		public static int GetLayerThick(double kValue, double standardValue, double thermal)
		{
			int num = 0;
			if (kValue > standardValue)
			{
				num = Convert.ToInt32(Math.Round(thermal * (kValue - standardValue) / (standardValue * kValue) * 1000.0, 0));
				if (num < 5)
				{
					num = 5;
				}
			}
			return num;
		}

		private void btnSet_Click(object sender, EventArgs e)
		{
			Transaction transaction = new Transaction(this.uiDoc.Document, "设置保温层厚度");
			try
			{
				IList<HeatRatio> list = this.SetHeatRatioByGridView(this.listNeedHR, this.dgvHeatRatio);
				int num = 0;
				foreach (HeatRatio heatRatio in list)
				{
					foreach (WallType wallType in this.wts)
					{
						if (heatRatio.WallName == wallType.Name && heatRatio.IsChecked)
						{
							num++;
						}
					}
				}
				DialogResult dialogResult = DialogResult.None;
				if (num > 0)
				{
					transaction.Start();
					foreach (HeatRatio heatRatio2 in list)
					{
						foreach (WallType wallType2 in this.wts)
						{
							if (heatRatio2.WallName == wallType2.Name && heatRatio2.IsChecked && heatRatio2.LayerThick > 0)
							{
								YJKRevitTools.SetWallLayerThick(wallType2, (double)heatRatio2.LayerThick, YJKRevitTools.GetAllMaterial(this.uiDoc.Document));
								heatRatio2.KValue = heatRatio2.StandardValue;
								break;
							}
						}
						WallInfoOperate.ModifyWallHeatRatio(heatRatio2);
					}
					transaction.Commit();
					YJKMessageBox.Show("修改保温层厚度完成！");
				}
				else
				{
					YJKMessageBox.Show("没有做任何修改！");
				}
				if (dialogResult == DialogResult.Yes)
				{
					base.Dispose();
				}
				else if (dialogResult == DialogResult.No)
				{
					return;
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				throw;
			}
			base.Close();
		}

		private void dgvHeatRatio_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			if (this.dgvHeatRatio.Columns[e.ColumnIndex].Name == "ColCheck")
			{
				e.Cancel = false;
				return;
			}
			e.Cancel = true;
		}

		private void dgvHeatRatio_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dgvHeatRatio.Columns[e.ColumnIndex].Name != "ColStandardRatio" || e.RowIndex < 0)
			{
				return;
			}
			FrmCheckWall frmCheckWall = new FrmCheckWall(this.uiDoc, true);
			if (frmCheckWall.ShowDialog() == DialogResult.Yes)
			{
				this.dgvHeatRatio.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = frmCheckWall.standardValue;
			}
		}

		private void dgvHeatRatio_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dgvHeatRatio.Columns[e.ColumnIndex].Name != "ColStandardRatio")
			{
				return;
			}
			double kValue = Convert.ToDouble(this.dgvHeatRatio.Rows[e.RowIndex].Cells["ColWallHeatRatio"].Value);
			double standardValue = Convert.ToDouble(this.dgvHeatRatio.Rows[e.RowIndex].Cells["ColStandardRatio"].Value);
			this.dgvHeatRatio.Rows[e.RowIndex].Cells["ColLayerThick"].Value = FrmSetInsulationLayer.GetLayerThick(kValue, standardValue, this.GetWallThermal(this.listHR));
		}

		private UIDocument uiDoc;

		private IList<HeatRatio> listHR = new List<HeatRatio>();

		private IList<HeatRatio> listNeedHR = new List<HeatRatio>();

		private List<WallType> wts = new List<WallType>();
	}
}
